package cn.quartz.gw.ex1;

import cn.quartz.DateUtil;
import org.quartz.*;

import java.util.Date;

import static org.quartz.DateBuilder.evenHourDate;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * @author : zhuqiang
 * @version : V1.0
 * @date : 2016/9/15 22:46
 */
public class Test {
    public static void main(String[] args) throws SchedulerException {
        SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();

        Scheduler sched = schedFact.getScheduler();

        sched.start();

        // define the job and tie it to our HelloJob class
        JobDetail job = JobBuilder.newJob(HelloJob.class)
                .withIdentity("myJob", "group1")
                .usingJobData("jobSays", "Hello World!")
                .usingJobData("myFloatValue", 3.141f)
                .build();

        // Trigger the job to run now, and then every 40 seconds
//        Trigger trigger = TriggerBuilder.newTrigger()
//                .withIdentity("myTrigger", "group1")
//                .startNow()
//                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
//                        .withIntervalInSeconds(10).repeatForever()  //间隔10秒钟，后面指定无限重复，否则只会运行一次
//                        )
//                .build();
        Trigger trigger = newTrigger()
                .withIdentity("trigger3", "group1")
                .startAt(new Date())  // 如果开始时间没有给出，那么就以当前时间
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(10)
                        .withRepeatCount(10)) // 注意，重复10次将总共11触发
                .forJob(job) // identify job with handle to its JobDetail itself
                .build();

        trigger = (SimpleTrigger) newTrigger()
                .withIdentity("trigger5", "group1")
                .startAt(DateBuilder.futureDate(10, DateBuilder.IntervalUnit.SECOND)) // 开始时间是未来五分钟
                .forJob(job)
                .build();

        // Tell quartz to schedule the job using our trigger
        sched.scheduleJob(trigger);
        System.out.println("- " + DateUtil.format(evenHourDate(null)));
    }
}
